
/**
 * @desc 将url跳转需要的obj对象转成string, 便于拼接在url上
 * 
 * @param {Object} obj  url参数对象
 * @returns {String} params url参数字符串
 * 
 * @demo
 *  @input {id: 2, url: 'http://www.baidu.com'}
 *  @program packingParams(input)
 *  @ouput id=2&url=http%3Awww.baidu.com
 */
function packingParams (obj) {
    if(obj && obj.constructor !== Object) return ''
    const array = Object.keys(obj).map(key => `${key}=${encodeURIComponent(obj[key])}`)
    return array.join('&')
}



/**
 * @desc  将url路径上拼接的参数转成参数对象
 * 
 * @param {String} urlParams  url参数字符串
 * @returns {String} params eg: {id: 2, url: 'http://www.baidu.com'}
 * 
 * @demo
 *  @input id=2&url=http%3Awww.baidu.com 
 *  @program analyParams(input)
 *  @ouput {id: 2, url: 'http://www.baidu.com'}
 */
 const analyParams = (url) => {
    const theRequest = {};
    if(!url) return theRequest
    const strs = url.split('&');
    for (let i = 0; i < strs.length; i++) {
        const tmp = strs[i].split('=');
        theRequest[tmp[0]] = decodeURIComponent(tmp[1]);
    }
    return theRequest;
};



export {
    packingParams,
    analyParams
}